import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { produce } from 'immer'

export type PageInfoStateType = {
  title: string
  desc?: string
  js?: string
  css?: string
  isPublished?: boolean
}

export const pageInitState: PageInfoStateType = {
  title: '',
  desc: '',
  js: '',
  css: '',
}

const pageSlice = createSlice({
  name: 'page',
  initialState: pageInitState,
  reducers: {
    resetPageInfo: (_state: PageInfoStateType, action: PayloadAction<PageInfoStateType>) => {
      return action.payload
    },

    //修改标题
    changePageTitle: produce((draft: PageInfoStateType, action: PayloadAction<string>) => {
      draft.title = action.payload
    }),
  },
})

export const { resetPageInfo, changePageTitle } = pageSlice.actions
export default pageSlice.reducer
